import React, { PureComponent } from "react";
import { Howl } from 'howler';
import {
  Footer,
  Header,
  Arwes,
  Image,
  Words,
  Project,
  Link
} from "arwes";
import { HttpClient } from '../commoe/HttpClient';
export default class blog extends PureComponent {
  constructor(props) {
    super(props);
    this.sounds = {
      click: new Howl({
        src: ['./sound/click.mp3']
      }),
      typing: new Howl({
        src: ['./sound/typing.mp3']
      }),
      deploy: new Howl({
        src: ['./sound/deploy.mp']
      }),
    }
    this.state = {
      show: true,
      bloglist: [],
    };
  }

  bindHandleScroll = (event) => {
    console.log(event)
    // 滚动的高度
    const scrollTop = (event.srcElement ? event.srcElement.documentElement.scrollTop : false)
      || window.pageYOffset
      || (event.srcElement ? event.srcElement.body.scrollTop : 0);
    console.log(scrollTop)
  }
  componentDidMount() {
    this.refs.bloglist.addEventListener('scroll', this.bindHandleScroll)
    console.log(this.refs.bloglist)
    const response = HttpClient.query('blog/getBlog?start=0&size=3', 'get')
    response().then(res => {
      if (res.code == 200) {
        this.setState({
          bloglist: res.data
        })
      }
    })
  }
  gohome = (url) => {
    if (this.props.location.pathname == url) {
      return
    }
    console.log(url)
    this.sounds.click.play()
    this.sounds.deploy.play()
    this.setState({
      show: !this.state.show
    });
    setTimeout(() => {
      this.props.history.push(url);
    }, 500);
  }
  render() {
    const iconmap = {
      "code": (<div><i className='mdi mdi-code-brackets' />&nbsp;Code</div>),
    }
    const bloglist = this.state.bloglist.map(option => (
      <Project
        style={{ marginTop: 20 }}
        onClick={option.url ? () => { window.location.href = option.url } : () => { }}
        animate
        headerSize='h3'
        icon={iconmap[option.icon]}
        header={option.title}
      >
        {anim => (
          <>
            <p>
              <Words animate show={anim.entered} layer='control' style={{ fontSize: 18, fontFamily: '站酷文艺体' }}>
                {option.body}
              </Words>
            </p>
            <p style={{ marginTop: 20, color: '#acf9fb' }}>
              {option.date ? (<><i className='mdi mdi-calendar' />&nbsp;{option.date}</>) : ''}
              {option.language ? (<><i style={{ marginLeft: 10 }} className='mdi mdi-file-document-box' />&nbsp;{option.language}</>) : ''}
            </p>
          </>
        )}
      </Project>
    ))
    return (
      <Arwes
        timeout={1500}
        animate
        show={this.state.show}
        background="./img/background-large.jpg"
        pattern="./img/glow.png"
        style={{ height: '100%' }}
      >
        <div style={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
          <Header animate show={this.state.show}>
            <div style={{
              margin: '0 auto',
              maxWidth: 800,
              padding: '20px 0',
              textAlign: 'left',
              position: 'relative'
            }}>
              <a style={{
                textAlign: 'left',
                width: '50%',
                float: 'left',
                padding: '0 10px',
                display: 'inline-block'
              }}>
                <Image
                  show={this.state.show}
                  onClick={() => this.gohome('/')}
                  layer={"header"}
                  animate
                  resources='./img/mine.jpeg'
                  style={{ width: 45, height: 45, margin: 0, display: 'inline-block' }}></Image>
                <Words
                  onClick={() => this.gohome('/')}
                  animate
                  show={this.state.show}
                  style={{
                    display: 'inline-block',
                    fontSize: 28,
                    color: '#a1ecfb',
                    fontFamily: 'Electrolize, sans-serif',
                    textShadow: '0 0 4px',
                    fontWeight: 'bold',
                    textTransform: 'uppercase',
                    margin: '0 0 0 10px',
                    paddingTop: 4,
                    lineHeight: 1,
                    verticalAlign: 'middle'
                  }}>
                  GUO WÉNJUN
                </Words>
              </a>
              <div style={{ textAlign: 'right', width: '50%', float: 'right', padding: '0 10px', marginLeft: 'auto' }}>
                <Link onClick={() => this.gohome('/blog')} style={{ fontFamily: "Electrolize,sans-serif", lineHeight: '45px' }}><i className="mdi mdi-note-outline" /> Blog</Link>
                <Link onClick={() => this.gohome('/paper')} style={{ marginLeft: 20, lineHeight: '45px' }}><i className="mdi mdi-box-cutter" /> Paper</Link>
                <Link onClick={() => this.gohome('/tools')} style={{ marginLeft: 20, lineHeight: '45px' }}><i className="mdi mdi-calculator" /> Tools</Link>
              </div>
              <div style={{ clear: 'both' }}></div>
            </div>
          </Header>
          <div
            style={{
              flex: '1',
              padding: '20px 0',
              overflowY: 'auto',
            }}
          >
            <div
              style={{
                margin: '0 auto',
                padding: '0 10px',
                maxWidth: 800,
                marginBottom: 0,
              }}
              ref="bloglist"
            >
              <div style={{ fontSize: 24 }}>
                <Link show={this.state.show}>
                  <i className="mdi mdi-chevron-double-right" />
                  <Words show={this.state.show} animate style={{ marginLeft: 5 }}>BLOG</Words>
                </Link>

                <Link style={{ float: 'right' }} show={this.state.show}>
                  <i className="mdi mdi-chevron-double-left" />
                </Link>
                <div style={{ clear: 'both' }}></div>
              </div>
              {bloglist}
              <div style={{height:100}}></div>
            </div>
          </div>
          <Footer animate show={this.state.show} style={{ textAlign: 'left' }}>
            <div style={{
              margin: '0 auto',
              maxWidth: 800,
              padding: '20px 0',
              textAlign: 'left',
              position: 'relative'
            }}>
              <Link href='https://gitee.com/mimidegongkai' target="_blank">
                <i className="mdi mdi-code-brackets" />
                <Words show={this.state.show} animate style={{ marginLeft: 5 }}>Codebase</Words>
              </Link>

              <Link style={{ float: 'right' }} href='http://beian.miit.gov.cn/' target="_blank">
                <Words show={this.state.show} animate style={{ marginLeft: 5 }}>京ICP备2021006930号</Words>
              </Link>
              <div style={{ clear: 'both' }}></div>
            </div>
          </Footer>
        </div>
      </Arwes >
    );
  }
}
